package com.drinks.yiniang.ui.widget.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * 两个dot虚线连接view
 *
 * @author han
 * @date 2019/11/19
 */
public class TwoDotLineView extends View {

    private int mLineColor;
    private int mLineHeight;

    private int mDotColor;
    private int mDotRadius;

    private Paint mLinePaint;
    private Paint mDotPaint;
    private Path mPath;

    public TwoDotLineView(Context context) {
        this(context, null);
    }

    public TwoDotLineView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TwoDotLineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init();
    }

    private void init() {
        mLineColor = Color.WHITE;
        mLineHeight = 3;

        mDotColor = mLineColor;
        mDotRadius = 12;

        mLinePaint = new Paint();
        mDotPaint = new Paint();

        mPath = new Path();

        mLinePaint.setStyle(Paint.Style.STROKE);
        //PathEffect是用来控制绘制轮廓(线条)的方式
        //代码中的float数组,必须是偶数长度,且>=2,指定了多少长度的实线之后再画多少长度的空白.
        //如本代码中,绘制长度5的实线,再绘制长度5的空白,再绘制长度5的实线,再绘制长度5的空白,依次重复.0是偏移量,可以不用理会.
        mLinePaint.setPathEffect(new DashPathEffect(new float[]{10, 5, 10, 5}, 0));
        mLinePaint.setColor(mLineColor);
        mLinePaint.setStrokeWidth(mLineHeight);
        mLinePaint.setAntiAlias(true);

        mDotPaint.setStyle(Paint.Style.FILL);
        mDotPaint.setColor(mLineColor);
        mDotPaint.setStrokeWidth(mLineHeight);
        mDotPaint.setAntiAlias(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = measureWidth(widthMeasureSpec);

        setMeasuredDimension(widthSize, mDotRadius * 2 > mLineHeight ? mDotRadius * 2 : mLineHeight);
    }

    private int measureWidth(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        //设置一个默认值，就是这个View的默认宽度为1，这个看我们自定义View的要求
        int result = 2;

        if (specMode == MeasureSpec.AT_MOST) {//相当于我们设置为wrap_content
            result = specSize;
        } else if (specMode == MeasureSpec.EXACTLY) {//相当于我们设置为match_parent或者为一个具体的值
            result = specSize;
        }

        return result > mDotRadius * 2 ? result : mDotRadius * 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPath.reset();
        mPath.moveTo(mDotRadius * 2, mDotRadius);
        mPath.lineTo(getWidth() - mDotRadius * 2, mDotRadius);

        canvas.drawPath(mPath, mLinePaint);

        int centX = mDotRadius;
        int centY = mDotRadius;

        canvas.drawCircle(centX, centY, mDotRadius, mDotPaint);

        centX = getWidth() - mDotRadius;

        canvas.drawCircle(centX, centY, mDotRadius, mDotPaint);
    }
}
